/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
import { TaskRunner } from "./concurrent/TaskRunner";
import { RealConnectionPool } from "./connection/RealConnectionPool";
import { TimeUnit } from "./utils/TimeUnit";

/**
 * Manages reuse of HTTP and HTTP/2 connections for reduced network latency. HTTP requests that
 * share the same [Address] may share a [Connection]. This class implements the policy
 * of which connections to keep open for future use.
 *
 * @constructor Create a new connection pool with tuning parameters appropriate for a single-user
 * application. The tuning parameters in this pool are subject to change in future OkHttp releases.
 * Currently this pool holds up to 5 idle connections which will be evicted after 5 minutes of
 * inactivity.
 */
export class ConnectionPool{
  delegate: RealConnectionPool

  constructor(maxIdleConnections?: number, keepAliveDuration?: number, timeUnit?: TimeUnit){
    if (maxIdleConnections === undefined && keepAliveDuration === undefined && timeUnit === undefined){
      this.delegate = new RealConnectionPool(TaskRunner.INSTANCE,5,5,TimeUnit.MINUTES)
      return this;
    }
    this.delegate = new RealConnectionPool(TaskRunner.INSTANCE,maxIdleConnections,keepAliveDuration,timeUnit)
    return this;
  }

  /** Returns the number of idle connections in the pool. */
  idleConnectionCount(): number {
    return this.delegate.idleConnectionCount()
  }

  /** Returns total number of connections in the pool. */
  connectionCount(): number {
    return this.delegate.connectionCount()
  }

  /** Close and remove all idle connections in the pool. */
  evictAll() {
    this.delegate.evictAll()
  }
}
